package com.asz.application
{
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import com.asz.application.ASZSite;
	import mx.core.Application;
	import com.asz.ASZPlane;
	
//Import Papervision Classes
       
     import org.papervision3d.scenes.*;
     import org.papervision3d.cameras.*;  
     import org.papervision3d.objects.*;
     import org.papervision3d.objects.primitives.*;
     import org.papervision3d.materials.*;
     import org.papervision3d.materials.shadematerials.*;
     import org.papervision3d.materials.utils.MaterialsList;
     import org.papervision3d.lights.*;
     import org.papervision3d.render.*;
     import org.papervision3d.view.*;
     import org.papervision3d.events.*;
     import org.papervision3d.core.*;
     import org.papervision3d.lights.PointLight3D;
     import flash.filters.DropShadowFilter;

	 import caurina.transitions.*;
	public class CarrousselApplication extends XMLLoadDataApplication
	{
		public function CarrousselApplication()
		{
			super();
		}
		
		
		 
		 private var siteList:Array=new Array();
		 
		 
		 public var dest:Number = 1;
	     private var theLight:PointLight3D;
	    
        //Papervision Engine
	    
	     private var viewport:Viewport3D;  
	     private var scene:Scene3D;  
	     private var camera:Camera3D; 
	     private var renderer:BasicRenderEngine; 
	      
         private var planeArray:Array = new Array();
         [Bindable]
         public var object:Object;
         
         
         private var arrayPlane:Object;
         private var p:ASZPlane;
		 private var anglePer:Number = 0;
  
	     	public var angleX:Number = anglePer;
	    
		 private var myCarousselParam:CarousselParam = new CarousselParam() ; 	
        
         //Initiation function	  
         protected override function init():void  {   	
         	super.init();
	         viewport = new Viewport3D(getCanvas().width, getCanvas().height, false, true);  
	         getCanvas().rawChildren.addChild(viewport);  
	         viewport.buttonMode=true;
	         
	         renderer = new BasicRenderEngine(); 
	         scene = new Scene3D();  
	         camera = new Camera3D();
	         camera.zoom = myCarousselParam.cameraZoom;  
	         //createObjects();  
	     } 
	     
		protected override function adSite(act:ASZSite):void {
				siteList.push(act);
		}
                 
		//Create Objects function           
		protected override function createObjects():void{
			if(siteList.length!=0)
				anglePer= (Math.PI*2) / siteList.length;
	         angleX=anglePer;
	         
			for(var i:uint=0; i<siteList.length; i++)
			{
				 var shadow:DropShadowFilter = new DropShadowFilter();
				shadow.distance = 10;
		        shadow.angle = 25;
				
				var bam:BitmapFileMaterial = new BitmapFileMaterial((siteList[i] as ASZSite ).imgUrl);
				
				bam.oneSide = false;
				bam.smooth = true;
				
		        bam.interactive = true;
				
				p = new ASZPlane(bam, Application.application.width/2, Application.application.height*0.4, 2, 2);
				p.x = Math.cos(i*anglePer) * myCarousselParam.radius;
				p.z = Math.sin(i*anglePer) * myCarousselParam.radius;
				p.rotationY = (-i*anglePer) * (180/Math.PI) + 270;
				//p.url = urlList[i-1];
				p.url =(siteList[i] as ASZSite ).url;
				p.name = (siteList[i] as ASZSite ).title;
				
				scene.addChild(p);
				//p.filters=[shadow];
				p.filters=[shadow];
				p.extra={pIdent:"in"};
				
				p.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, openURL);
				p.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, toggler);
				p.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, toggler);
		                                             
		        planeArray[i] = p;
			}
			
			// create lighting
			    theLight = new PointLight3D();
		        scene.addChild(theLight);
		        theLight.y = getCanvas().height;
		        
	         addEventListeners();
		}   
	
		private function openURL(event:InteractiveScene3DEvent):void        {
                var monPane:ASZPlane = event.target as ASZPlane;
                trace("url="+monPane.url);
                
				navigateToURL(new URLRequest(monPane.url),'_self');
        }
       
		private function toggler(event:InteractiveScene3DEvent):void        {
                // if the cube's position is "in", move it out else move it back
                if (event.target.extra.pIdent == "in") {
                        moveOut(event.target);
                } else  {   
                        moveBack(event.target);
                }
        }
       
        private function moveOut(object:Object):void
        {
        	trace(object +" my object");
        	
                // for each cube that was not selected, remove the click event listener
                for each (var arrayPlane:Object in planeArray)
                {
                        if (arrayPlane != object)
                        {
                                arrayPlane.removeEventListener(InteractiveScene3DEvent.OBJECT_PRESS, toggler);
                        }
                }
                
                // move the selected cube out 1000 and rotate 90 degrees once it has finished moving out
                
                Tweener.addTween(object, {scaleX:myCarousselParam.overZoomFactor, time:myCarousselParam.timeReaction, transition:"easeInCirc", onComplete:rotateCube, onCompleteParams:[object]});
                Tweener.addTween(object, {scaleY:myCarousselParam.overZoomFactor, time:myCarousselParam.timeReaction, transition:"easeInCirc", onComplete:rotateCube, onCompleteParams:[object]});
                // set the cube's position to "out"
                object.extra = {pIdent:"out"};
                
                // move the camera out 1000 and move it the to same y coordinate as the selected cube
                //Tweener.addTween(camera, {x:1000, y:object.y, rotationX:0, time:0.5, transition:"easeInOutSine"});
        }
               
        private function moveBack(object:Object):void
        {
                // for each cube that was not selected, add the click event listener back
                for each (var arrayPlane:Object in planeArray) {
                        if (arrayPlane != object) {
                                arrayPlane.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, toggler);
                        }
                }
               // move the selected cube back to 0 and rotate 90 degrees once it has finished moving back
                Tweener.addTween(object, {scaleX:1, time:myCarousselParam.timeReaction, transition:"easeInCirc", onComplete:rotateCube, onCompleteParams:[object]});
                Tweener.addTween(object, {scaleY:1, time:myCarousselParam.timeReaction, transition:"easeInCirc", onComplete:rotateCube, onCompleteParams:[object]});
       
                // set the cube's position to "in"
                object.extra = {pIdent:"in"};
         }
        
        private function goBack():void {
                // for each cube that was not selected, add the click event listener back
                for each (var arrayPlane:Object in planeArray){
                        if (arrayPlane != object){
                                arrayPlane.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, toggler);
                                //moveBack(arrayPlane);
                        }
                }
        }
        
        private function rotateCube(object:Object):void        {
                object.rotationX = 0;
                Tweener.addTween(object, {rotationZ:0, time:myCarousselParam.timeReaction, transition:"easeOutElastic"});
        }
	
		private function addEventListeners():void{
	   		 this.addEventListener(Event.ENTER_FRAME, render);
	   		 this.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelMgt);
		}
	      
		private function mouseWheelMgt(e:MouseEvent):void{  
	       //e.stopPropagation();
	       if(e.ctrlKey){
	       		if(e.delta>0) zoomIn();
	       		else zoomOut();
	       }else{	       
		       if(e.delta>0) moveRight();
		       else moveLeft();
	       }
         } 
//Enter Frame Listener function              
       
		private function render(e:Event):void{  
	       renderer.renderScene(scene, camera, viewport);
	       
			camera.x = Math.cos(angleX) * 800;					 
	       camera.z = Math.sin(angleX) * 800;
         }
         
         protected function zoomIn():void	{   
			getCanvas().scaleX = Math.min(5,getCanvas().scaleX*myCarousselParam.zoomFactor);
			getCanvas().scaleY = Math.min(5,getCanvas().scaleY*myCarousselParam.zoomFactor);
			centerCanvas();
		}
		
		protected function zoomOut():void	{   
			getCanvas().scaleX = Math.max(0.05, getCanvas().scaleX/myCarousselParam.zoomFactor);
			getCanvas().scaleY = Math.max(0.05,getCanvas().scaleY/myCarousselParam.zoomFactor);
			centerCanvas();
		}
		
		private function centerCanvas():void{ 
			//getCanvas().x = getCanvas().parent.width* 0.5 -getCanvas().width /getCanvas().scaleX;
			getCanvas().x = getCanvas().parent.width* 0.5 -getCanvas().width/2 ;
		}
		protected function moveRight():void
		{   
			dest++;
			//Tweener.addTween(this, {angleX:dest*anglePer, time:0.5});
			Tweener.addTween(this, {angleX:dest*anglePer, time:myCarousselParam.timeReaction, transition:"easeOutElastic"});
			goBack();
		}
		
		protected function moveLeft():void
		{
			dest--;
			//Tweener.addTween(this, {angleX:dest*anglePer, time:0.5});
			Tweener.addTween(this, {angleX:dest*anglePer, time:myCarousselParam.timeReaction, transition:"easeOutElastic"});
			goBack();
		}   
  		protected function getCanvas():Canvas
		{
			Alert.show("methode getCanvas a surcharger");
			return null;
		}
	}
}